package com.eyeem.recyclerviewtools.sample;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.eyeem.recyclerviewtools.LoadMoreOnScrollListener;
import com.eyeem.recyclerviewtools.StaggeredLayoutManagerUtils;
import com.eyeem.recyclerviewtools.adapter.WrapAdapter;
import com.eyeem.recyclerviewtools.sample.adapter.DataAdapter;
import com.eyeem.recyclerviewtools.sample.data.Data;

public class HeaderBehaivorActivity extends BaseActivity implements LoadMoreOnScrollListener.Listener {
    private SwipeRefreshLayout swipeRefreshLayout;
    private DataAdapter adapter;
    private WrapAdapter wrapAdapter;
    private RecyclerView recycler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_header_behaivor);
        setTitle(null);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //添加菜单和返回按钮
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        recycler = (RecyclerView) findViewById(R.id.recycler);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        // Adapter
        adapter = DataAdapter.generateRandom(this);
        wrapAdapter = new WrapAdapter(adapter);
        // Layout manager
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recycler.setLayoutManager(layoutManager);
        recycler.setAdapter(wrapAdapter);

        //NOtice: 使用QuickReturnHeaderBehavior的时候，会导致QuickReturnHeader遮挡RecyclerView，解决方法：
        //1、如果有addHeader，则HeaderView 的布局 要设置marginTop 为QuickReturnHeader 的高度；
        //2、没有addHeadr,则
        // (1)addHeader，则HeaderView 的布局要设置layout_height 为QuickReturnHeader 的高度
        // (2)RecyclerView的第一项要设置marginTop为QuickReturnHeader 的高度
        // Header
        View header = getLayoutInflater().inflate(R.layout.view_recyler_header, null);
        wrapAdapter.addHeader(header);


        // Load more scroller
        recycler.addOnScrollListener(new LoadMoreOnScrollListener(this));
        Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                String msg = "";
                switch (menuItem.getItemId()) {
                    case R.id.action_settings:
                        msg += "设置";
                        break;
                }

                if (!msg.equals("")) {
                    Toast.makeText(HeaderBehaivorActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        };
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
        dismissRefresh(swipeRefreshLayout);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_space, menu);
        return true;
    }

    @Override
    public void onLoadMore(RecyclerView recyclerView) {
        wrapAdapter.showLoadMore();
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {

                for (int i = 0; i < 5; i++) {
                    adapter.data.add(Data.newRandom(HeaderBehaivorActivity.this));
                }

                if (recycler.getLayoutManager() instanceof StaggeredGridLayoutManager) {
                    StaggeredLayoutManagerUtils.onItemChanged(recycler, 0);
                }
                adapter.notifyDataSetChanged();
                wrapAdapter.hideLoadMore();

            }
        }, 1000);
    }
}
